package com.teachoa.web;

import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.teachoa.bean.TeacherStudent;
import com.teachoa.bean.User;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.SexEnum;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.DictUtil;
import com.teachoa.util.IDUtil;

public class TeachController extends BaseController {
	
	public void index() {
		int pageNo = getParaToInt("pageNo", 1);
		Page<User> teacherPage = User.dao.getUserPageByType(pageNo, UserTypeEnum.TEACHER.getCode());
		Map<Integer, String> gradeMap = DictUtil.getDictMapByPath(Global.gradePath);
		Map<Integer, String> majorMap = DictUtil.getDictMapByPath(Global.majorPath);
		for(User teacher:teacherPage.getList()) {
			teacher.put("sex_display", SexEnum.getEnumByCode(teacher.getInt("sex")).getDesc());
			teacher.put("type_display", UserTypeEnum.getEnumByCode(teacher.getInt("type")).getDesc());
			teacher.put("grade_display", gradeMap.get(teacher.getInt("grade")));
			teacher.put("major_display", majorMap.get(teacher.getInt("major")));
		}
		setAttr("teacherPage", teacherPage);
		
//		Page<User> studentPage = User.dao.getUserPageByType(pageNo, UserTypeEnum.STUDENT.getCode());
//		for(User student:studentPage.getList()) {
//			student.put("sex_display", sexMap.get(student.getInt("sex")));
//			student.put("type_display", UserTypeEnum.getEnumByCode(student.getInt("type")).getDesc());
//			student.put("grade_display", gradeMap.get(student.getInt("grade")));
//			student.put("major_display", majorMap.get(student.getInt("major")));
//		}
//		setAttr("studentPage", studentPage);
		renderJsp("teach.jsp");
	}
	
	public void load() {
		setAttr("user", User.dao.findById(getPara()));
		setAttr("sexs", DictUtil.getSubDictsByPath(Global.sexPath));
		setAttr("grades", DictUtil.getSubDictsByPath(Global.gradePath));
		setAttr("majors", DictUtil.getSubDictsByPath(Global.majorPath));
		renderJsp("user_view.jsp");
	}
	
	public void loadStudents() {
		String teacherId = getPara();
		int pageNo = getParaToInt("pageNo", 1);
		Page<User> studentPage = User.dao.getStudentsByTeacherId(pageNo, teacherId);
		setAttr("studentPage", studentPage);
		renderJsp("student_table.jsp");
	}
	
	public void studentList() {
		keepPara("teacherId");
		int pageNo = getParaToInt("pageNo", 1);
		setAttr("grades", DictUtil.getSubDictsByPath(Global.gradePath));
		setAttr("majors", DictUtil.getSubDictsByPath(Global.majorPath));
		Page<User> studentPage = User.dao.getUserPageByType(pageNo, UserTypeEnum.STUDENT.getCode());
		setAttr("studentPage", studentPage);
		renderJsp("student_list.jsp");
	}
	
	public void selectStudents() {
		Page<User> studentPage = User.dao.selectStudentsByCondition(getParamMap());
		setAttr("studentPage", studentPage);
		renderJsp("select_student_table.jsp");
	}
	
	public void saveRelation() {
		TeacherStudent ts = getModel(TeacherStudent.class);
		ts.set("id", IDUtil.getId());
		
		TeacherStudent ts2 = TeacherStudent.dao.getRelationBytidAndsid(ts.getStr("teacher_id"), ts.getStr("student_id"));
		if(ts2!=null) {
			renderJson(fail("添加失败！！"));
		} else {
			try {
				ts.save();
				renderJson(success("添加成功！！"));
			} catch (Exception e) {
				e.printStackTrace();
				renderJson(fail("添加失败！！"));
			}
		}
	}
	
	public void deleteRelation() {
		String studentId = getPara(0);
		String teacherId = getPara(1);
		try {
			TeacherStudent.dao.deleteByStudentIdAndTeacherId(studentId, teacherId);
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
		
	}

}
